/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ConsultaPrecio;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author nan
 */

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class MedConsultaPrecio implements ActionListener, KeyListener {

    private ConsultaPrecioGUI consulta = new ConsultaPrecioGUI();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private String tipo;
    private double porsentaje=0;

    public MedConsultaPrecio(){
        consulta.setLocationRelativeTo(null);
        consulta.setActionListeners(this);
        consulta.setKeyListener(this);
        consulta.show();

    }

    public void keyTyped(KeyEvent e) {
        if (e.getSource() == consulta.getjTextFieldCodigo() && e.getKeyChar() == KeyEvent.VK_ENTER){
            consulta.getjButtonAceptar().doClick();
        }
        if (e.getSource()== consulta.getjTextFieldCodigo()){
            char caracter = e.getKeyChar();
            if((caracter == '$')){
                e.consume();
            }
        }
    }

    public void keyPressed(KeyEvent e) {
        
    }

    public void keyReleased(KeyEvent e) {}

    public void actionPerformed(ActionEvent e) {
        this.tipo = "Tarjeta";
        if (e.getSource() == consulta.getjButtonAceptar()){
            Consultar();
        }
        if(e.getSource() == consulta.getjButtonCancelar()){
            consulta.getjTextFieldCodigo().setText("");
            consulta.getjLabelDescripcion().setText("-------");
            consulta.getjLabelNombre().setText("-------");
            consulta.getjLabelMarca().setText("-------");
            consulta.getjLabelTarjeta().setText("-------");
            consulta.getjLabelContado().setText("-------");
            consulta.dispose();

        }
    }

    public void setVisible(boolean mostrar){
        consulta.setVisible(mostrar);
    }

    public void Consultar(){
        String prim = (consulta.getjTextFieldCodigo().getText());
        conectar.conectar();
        Statement st;
        try {
            st = conectar.getConn().createStatement();
            String consulId = "SELECT \"codArt\", nombre, marca, descripcion,\"precioCosto\" FROM \"Articulo\"";
            String consulta1 = "SELECT * FROM \"TipoPago\" WHERE \"Tipo\" = '"+tipo+"';";
            ResultSet rs = st.executeQuery(consulta1);
            while (rs.next()){
                porsentaje = rs.getDouble(2);
            }
            rs = st.executeQuery(consulId);
            String id = null;
            int i = 0;
            while (rs.next()){
                id = rs.getString(1);
                if (id.matches(prim)){
                    break;
                }
                i=i+1;
            }
            if(id==null){
                JOptionPane.showMessageDialog(null, "Ingrese un codigo","Error por campo vacio", JOptionPane.ERROR_MESSAGE);
            }else{
                if(id.matches(prim)){
                    consulta.getjLabelNombre().setText(rs.getString(2));
                    consulta.getjLabelMarca().setText(rs.getString(3));
                    consulta.getjLabelDescripcion().setText(rs.getString(4));
                    consulta.getjLabelTarjeta().setText(Math.rint(rs.getDouble(5)*porsentaje)+"");
                    consulta.getjLabelContado().setText(rs.getString(5));
                 }else{
                    JOptionPane.showMessageDialog(null, "Ingrese codigo de articulo","Error con articulo", JOptionPane.ERROR_MESSAGE);
                 }
            }
            rs.close();
            st.close();
            conectar.cerrar();
        } catch (SQLException ex) {

        }
    }




   
}
